package com.zzh.bc.modules.project.controller;

import com.zzh.bc.model.ProjectModel;
import com.zzh.bc.modules.project.vo.ProjectVo;
import com.zzh.bc.service.project.ProjectService;
import com.zzh.bc.service.project.impl.ProjectServiceImpl;
import com.zzh.bc.utils.RequestParamUtils;
import com.zzh.bc.utils.RespParamUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.util.List;

/**
 * 添加项目
 */
@WebServlet(name = "ProjectController")
public class ProjectController extends HttpServlet {
    private ProjectService projectService;
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ProjectVo vo = RequestParamUtils.requestBodyParams(request, ProjectVo.class);
        switch (vo.getMethod()) {
            case ProjectVo.PROJECT_FIND_ALL:
                findAll(vo, response);
                break;
            case ProjectVo.PROJECT_ADD:
                addProject(vo, response);
                break;
            case ProjectVo.PROJECT_DELETE:
                deleteProject(vo, response);
                break;
            default:
                break;
        }
    }

    private void deleteProject(ProjectVo vo, HttpServletResponse response) {

        if (StringUtils.isEmpty(vo.getCreateUserId()) || StringUtils.isEmpty(vo.getProjectId())) {
            System.out.println("参数错误");
            RespParamUtils.respJsonError(response);
            return;
        }

        checkProjectService();
        boolean b = projectService.deleteProject(vo.getCreateUserId(), vo);
        if (b) {
            RespParamUtils.respJsonOk(response, null);
        } else {
            RespParamUtils.respJsonError(response);
        }

    }

    private void addProject(ProjectVo vo, HttpServletResponse response) {
        if (vo == null || StringUtils.isEmpty(vo.getCreateUserId())
                || StringUtils.isEmpty(vo.getProjectName())
                || StringUtils.isEmpty(vo.getProjectBaseUrl())) {
            System.out.println("参数错误");
            RespParamUtils.respJsonError(response);
            return;
        }
        checkProjectService();
        boolean result = projectService.saveProject(vo);

        if (result) {
            RespParamUtils.respJsonOk(response, null);
        } else {
            RespParamUtils.respJsonError(response);
        }
    }

    //校验ProjectService是否为空
    private void checkProjectService() {
        if (projectService == null) {
            projectService = new ProjectServiceImpl();
        }
    }

    private void findAll(ProjectVo vo, HttpServletResponse response) {
        checkProjectService();
        List<ProjectModel> project = projectService.findAllProject(vo.getPage(), vo.getPageSize(), vo.getCreateUserId());
        try {
            RespParamUtils.respJsonOk(response, project);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }


}
